<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      label-position="top"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="loading"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userLogin } from "@/api";
export default {
  data() {
    return {
      loading: false,
      ruleForm: { name: "", password: "" },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          userLogin(this.ruleForm).then((res) => {
            if (res.code == 0) {
              this.$message({
                message: res.mes,
                type: "success",
              });
              localStorage.setItem("user", JSON.stringify(res.data));
              location.href = "/";
              // this.$router.push("/");
            } else {
              this.$message.error(res.mes);
            }
            this.loading = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="stylus" >
.demo-ruleForm {
  padding: 0px 20px;
}

.el-tabs__nav {
  border-left: none !important;
  border-top: none !important;
}
</style>